home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / parse.test < prev    next >
Encoding:
Text File  |  1995-06-29  |  11.3 KB  |  421 lines

  1. # Commands covered:  set (plus basic command syntax)
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) parse.test 1.29 95/06/29 13:39:15
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. proc fourArgs {a b c d} {
  18.     global arg1 arg2 arg3 arg4
  19.     set arg1 $a
  20.     set arg2 $b
  21.     set arg3 $c
  22.     set arg4 $d
  23. }
  24.  
  25. proc getArgs args {
  26.     global argv
  27.     set argv $args
  28. }
  29.  
  30. # Basic argument parsing.
  31.  
  32. test parse-1.1 {basic argument parsing} {
  33.     set arg1 {}
  34.     fourArgs a b    c          d
  35.     list $arg1 $arg2 $arg3 $arg4
  36. } {a b c d}
  37. test parse-1.2 {basic argument parsing} {
  38.     set arg1 {}
  39.     eval "fourArgs 123\v4\f56\r7890"
  40.     list $arg1 $arg2 $arg3 $arg4
  41. } {123 4 56 7890}
  42.  
  43. # Quotes.
  44.  
  45. test parse-2.1 {quotes and variable-substitution} {
  46.     getArgs "a b c" d
  47.     set argv
  48. } {{a b c} d}
  49. test parse-2.2 {quotes and variable-substitution} {
  50.     set a 101
  51.     getArgs "a$a b c"
  52.     set argv
  53. } {{a101 b c}}
  54. test parse-2.3 {quotes and variable-substitution} {
  55.     set argv "xy[format xabc]"
  56.     set argv
  57. } {xyxabc}
  58. test parse-2.4 {quotes and variable-substitution} {
  59.     set argv "xy\t"
  60.     set argv
  61. } xy\t
  62. test parse-2.5 {quotes and variable-substitution} {
  63.     set argv "a b    c
  64. d e f"
  65.     set argv
  66. } a\ b\tc\nd\ e\ f
  67. test parse-2.6 {quotes and variable-substitution} {
  68.     set argv a"bcd"e
  69.     set argv
  70. } {a"bcd"e}
  71.  
  72. # Braces.
  73.  
  74. test parse-3.1 {braces} {
  75.     getArgs {a b c} d
  76.     set argv
  77. } "{a b c} d"
  78. test parse-3.2 {braces} {
  79.     set a 101
  80.     set argv {a$a b c}
  81.     set b [string index $argv 1]
  82.     set b
  83. } {$}
  84. test parse-3.3 {braces} {
  85.     set argv {a[format xyz] b}
  86.     string length $argv
  87. } 15
  88. test parse-3.4 {braces} {
  89.     set argv {a\nb\}}
  90.     string length $argv
  91. } 6
  92. test parse-3.5 {braces} {
  93.     set argv {{{{}}}}
  94.     set argv
  95. } "{{{}}}"
  96. test parse-3.6 {braces} {
  97.     set argv a{{}}b
  98.     set argv
  99. } "a{{}}b"
  100. test parse-3.7 {braces} {
  101.     set a [format "last]"]
  102.     set a
  103. } {last]}
  104.  
  105. # Command substitution.
  106.  
  107. test parse-4.1 {command substitution} {
  108.     set a [format xyz]
  109.     set a
  110. } xyz
  111. test parse-4.2 {command substitution} {
  112.     set a a[format xyz]b[format q]
  113.     set a
  114. } axyzbq
  115. test parse-4.3 {command substitution} {
  116.     set a a[
  117. set b 22;
  118. format %s $b
  119.  
  120. ]b
  121.     set a
  122. } a22b
  123.  
  124. # Variable substitution.
  125.  
  126. test parse-5.1 {variable substitution} {
  127.     set a 123
  128.     set b $a
  129.     set b
  130. } 123
  131. test parse-5.2 {variable substitution} {
  132.     set a 345
  133.     set b x$a.b
  134.     set b
  135. } x345.b
  136. test parse-5.3 {variable substitution} {
  137.     set _123z xx
  138.     set b $_123z^
  139.     set b
  140. } xx^
  141. test parse-5.4 {variable substitution} {
  142.     set a 78
  143.     set b a${a}b
  144.     set b
  145. } a78b
  146. test parse-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
  147. test parse-5.6 {variable substitution} {
  148.     catch {$_non_existent_} msg
  149.     set msg
  150. } {can't read "_non_existent_": no such variable}
  151. test parse-5.7 {array variable substitution} {
  152.     catch {unset a}
  153.     set a(xyz) 123
  154.     set b $a(xyz)foo
  155.     set b
  156. } 123foo
  157. test parse-5.8 {array variable substitution} {
  158.     catch {unset a}
  159.     set "a(x y z)" 123
  160.     set b $a(x y z)foo
  161.     set b
  162. } 123foo
  163. test parse-5.9 {array variable substitution} {
  164.     catch {unset a}; catch {unset qqq}
  165.     set "a(x y z)" qqq
  166.     set $a([format x]\ y [format z]) foo
  167.     set qqq
  168. } foo
  169. test parse-5.10 {array variable substitution} {
  170.     catch {unset a}
  171.     list [catch {set b $a(22)} msg] $msg
  172. } {1 {can't read "a(22)": no such variable}}
  173. test parse-5.11 {array variable substitution} {
  174.     set b a$!
  175.     set b
  176. } {a$!}
  177. test parse-5.12 {array variable substitution} {
  178.     set b a$()
  179.     set b
  180. } {a$()}
  181. catch {unset a}
  182. test parse-5.13 {array variable substitution} {
  183.     catch {unset a}
  184.     set long {This is a very long variable, long enough to cause storage \
  185.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  186.     freed up correctly, then a core leak will occur when this test is \
  187.     run.  This text is probably beginning to sound like drivel, but I've \
  188.     run out of things to say and I need more characters still.}
  189.     set a($long) 777
  190.     set b $a($long)
  191.     list $b [array names a]
  192. } {777 {{This is a very long variable, long enough to cause storage \
  193.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  194.     freed up correctly, then a core leak will occur when this test is \
  195.     run.  This text is probably beginning to sound like drivel, but I've \
  196.     run out of things to say and I need more characters still.}}}
  197. test parse-5.14 {array variable substitution} {
  198.     catch {unset a}; catch {unset b}; catch {unset a1}
  199.     set a1(22) foo
  200.     set a(foo) bar
  201.     set b $a($a1(22))
  202.     set b
  203. } bar
  204. catch {unset a}; catch {unset a1}
  205.  
  206. # Backslash substitution.
  207.  
  208. set errNum 1
  209. proc bsCheck {char num} {
  210.     global errNum
  211.     test parse-6.$errNum {backslash substitution} {
  212.     scan $char %c value
  213.     set value
  214.     } $num
  215.     set errNum [expr $errNum+1]
  216. }
  217.  
  218. bsCheck \b    8
  219. bsCheck \e    101
  220. bsCheck \f    12
  221. bsCheck \n    10
  222. bsCheck \r    13
  223. bsCheck \t    9
  224. bsCheck \v    11
  225. bsCheck \{    123
  226. bsCheck \}    125
  227. bsCheck \[    91
  228. bsCheck \]    93
  229. bsCheck \$    36
  230. bsCheck \     32
  231. bsCheck \;    59
  232. bsCheck \\    92
  233. bsCheck \Ca    67
  234. bsCheck \Ma    77
  235. bsCheck \CMa    67
  236. bsCheck \8a    8
  237. bsCheck \14    12
  238. bsCheck \141    97
  239. bsCheck \340    224
  240. bsCheck b\0    98
  241. bsCheck \x    120
  242. bsCheck \xa    10
  243. bsCheck \x41    65
  244. bsCheck \x541    65
  245.  
  246. test parse-6.1 {backslash substitution} {
  247.     set a "\a\c\n\]\}"
  248.     string length $a
  249. } 5
  250. test parse-6.2 {backslash substitution} {
  251.     set a {\a\c\n\]\}}
  252.     string length $a
  253. } 10
  254. test parse-6.3 {backslash substitution} {
  255.     set a "abc\
  256. def"
  257.     set a
  258. } {abc def}
  259. test parse-6.4 {backslash substitution} {
  260.     set a {abc\
  261. def}
  262.     set a
  263. } {abc def}
  264. test parse-6.5 {backslash substitution} {
  265.     set msg {}
  266.     set a xxx
  267.     set error [catch {if {24 < \
  268.     35} {set a 22} {set \
  269.         a 33}} msg]
  270.     list $error $msg $a
  271. } {0 22 22}
  272. test parse-6.6 {backslash substitution} {
  273.     eval "concat abc\\"
  274. } "abc\\"
  275. test parse-6.7 {backslash substitution} {
  276.     eval "concat \\\na"
  277. } "a"
  278. test parse-6.8 {backslash substitution} {
  279.     eval "concat x\\\n       \na"
  280. } "x a"
  281. test parse-6.9 {backslash substitution} {
  282.     eval "concat \\x"
  283. } "x"
  284. test parse-6.10 {backslash substitution} {
  285.     eval "list a b\\\nc d"
  286. } {a b c d}
  287. test parse-6.11 {backslash substitution} {
  288.     eval "list a \"b c\"\\\nd e"
  289. } {a {b c} d e}
  290.  
  291. # Semi-colon.
  292.  
  293. test parse-7.1 {semi-colons} {
  294.     set b 0
  295.     getArgs a;set b 2
  296.     set argv
  297. } a
  298. test parse-7.2 {semi-colons} {
  299.     set b 0
  300.     getArgs a;set b 2
  301.     set b
  302. } 2
  303. test parse-7.3 {semi-colons} {
  304.     getArgs a b ; set b 1
  305.     set argv
  306. } {a b}
  307. test parse-7.4 {semi-colons} {
  308.     getArgs a b ; set b 1
  309.     set b
  310. } 1
  311.  
  312. # The following checks are to ensure that the interpreter's result
  313. # gets re-initialized by Tcl_Eval in all the right places.
  314.  
  315. test parse-8.1 {result initialization} {concat abc} abc
  316. test parse-8.2 {result initialization} {concat abc; proc foo {} {}} {}
  317. test parse-8.3 {result initialization} {concat abc; proc foo {} $a} {}
  318. test parse-8.4 {result initialization} {proc foo {} [concat abc]} {}
  319. test parse-8.5 {result initialization} {concat abc; } abc
  320. test parse-8.6 {result initialization} {
  321.     eval {
  322.     concat abc
  323. }} abc
  324. test parse-8.7 {result initialization} {} {}
  325. test parse-8.8 {result initialization} {concat abc; ; ;} abc
  326.  
  327. # Syntax errors.
  328.  
  329. test parse-9.1 {syntax errors} {catch "set a \{bcd" msg} 1
  330. test parse-9.2 {syntax errors} {
  331.     catch "set a \{bcd" msg
  332.     set msg
  333. } {missing close-brace}
  334. test parse-9.3 {syntax errors} {catch {set a "bcd} msg} 1
  335. test parse-9.4 {syntax errors} {
  336.     catch {set a "bcd} msg
  337.     set msg
  338. } {missing "}
  339. test parse-9.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
  340. test parse-9.6 {syntax errors} {
  341.     catch {set a "bcd"xy} msg
  342.     set msg
  343. } {extra characters after close-quote}
  344. test parse-9.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
  345. test parse-9.8 {syntax errors} {
  346.     catch "set a {bcd}xy" msg
  347.     set msg
  348. } {extra characters after close-brace}
  349. test parse-9.9 {syntax errors} {catch {set a [format abc} msg} 1
  350. test parse-9.10 {syntax errors} {
  351.     catch {set a [format abc} msg
  352.     set msg
  353. } {missing close-bracket}
  354. test parse-9.11 {syntax errors} {catch gorp-a-lot msg} 1
  355. test parse-9.12 {syntax errors} {
  356.     catch gorp-a-lot msg
  357.     set msg
  358. } {invalid command name "gorp-a-lot"}
  359. test parse-9.13 {syntax errors} {
  360.     set a [concat {a}\
  361.  {b}]
  362.     set a
  363. } {a b}
  364.  
  365. # Long values (stressing storage management)
  366.  
  367. set a {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH}
  368.  
  369. test parse-10.1 {long values} {
  370.     string length $a
  371. } 214
  372. test parse-10.2 {long values} {
  373.     llength $a
  374. } 43
  375. test parse-1a1.3 {long values} {
  376.     set b "1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH"
  377.     set b
  378. } $a
  379. test parse-10.3 {long values} {
  380.     set b "$a"
  381.     set b
  382. } $a
  383. test parse-10.4 {long values} {
  384.     set b [set a]
  385.     set b
  386. } $a
  387. test parse-10.5 {long values} {
  388.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  389.     string length $b
  390. } 214
  391. test parse-10.6 {long values} {
  392.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  393.     llength $b
  394. } 43
  395. test parse-10.7 {long values} {
  396.     set b
  397. } $a
  398. test parse-10.8 {long values} {
  399.     set a [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ]
  400.     llength $a
  401. } 62
  402. set i 0
  403. foreach j [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ] {
  404.     set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
  405.     set test $test$test$test$test
  406.     set i [expr $i+1]
  407.     test parse-10.9 {long values} {
  408.     set j
  409.     } $test
  410. }
  411. test parse-10.10 {test buffer overflow in backslashes in braces} {
  412.     expr {"a" == {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101}}
  413. } 0
  414.  
  415. test parse-11.1 {comments at the end of a bracketed script} {
  416.     set x "[
  417. expr 1+1
  418. # skip this!
  419. ]"
  420. } {2}
  421.